package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.user.UserDO;
import com.qk.dto.user.UserDTO;
import com.qk.entity.User;
import com.qk.management.aop.anno.LogAnno;
import com.qk.management.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
@SuppressWarnings("all")
public class UserController {

    @Autowired
    private UserService userService;
    @GetMapping
    public Result listUser(UserDTO userDto){

        PageResult<UserDO> pageResult = userService.listUsers(userDto);
        return Result.success(pageResult);
    }

    @LogAnno
    @PostMapping
    public Result addUser(@RequestBody User user){
        userService.addUser(user);
        return Result.success();
    }

    @LogAnno
    @PutMapping
    public Result updateUser(@RequestBody User user){
        userService.updateUser(user);
        return Result.success();
    }

    @LogAnno
    @DeleteMapping("/{ids}")
    public Result deleteUser(@PathVariable("ids") List<Integer> ids){
        userService.deleteUser(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id){
        User user = userService.getById(id);
        return Result.success(user);
    }

    @GetMapping("/list")
    public Result getAll(){
        List<User> list = userService.getAll();
        return Result.success(list);
    }

    @GetMapping("/dept/{deptId}")
    public Result getByDeptId(@PathVariable("deptId") Integer deptId){
        List<User> list = userService.getByDeptId(deptId);
        return Result.success(list);
    }

    @GetMapping("/role/{roleLabel}")
    public Result getByRoleLabel(@PathVariable("roleLabel")String roleLabel){
        List<User> list = userService.getByRoleLabel(roleLabel);
        return Result.success(list);
    }
}
